<?php

include_once "src/controller/dbmanagement/ResourceDBManagement.php";

/**
 * It includes all the functions for the management of the business logic
 * of the Resources.
 */
class ResourceManagement
{

    /**
     *  Returns Resource Object with resource information in it if the resource
     *  table contains a resource with the resource id
     */
    public static function getResourceById($resourceId)
    {
        try
        {
            $resource = new Resource();
            $resource = ResourceDBManagement::findByResourceId($resourceId);
            return $resource;
        }
        catch (DBException $e)
        {
            throw $e;
        }
        catch (DBConnectionException $e)
        {
            throw $e;
        }
    }

    /**
     *  Returns Resource Object with resource information in it if the resource
     *  table contains a resource with the owner id of $ownerId.
     */

    public static function getResourceByOwnerId($ownerId)
    {
        try
        {
            $resource = new Resource();
            $resource = ResourceDBManagement::findByOwnerId($ownerId);
            return $resource;
        }
        catch (DBException $e)
        {
            throw $e;
        }
        catch (DBConnectionException $e)
        {
            throw $e;
        }

    }

    /**
     *  Returns true if database successfully completes delete on
     *  resource with the Resource Id of $resourceId. Else returns false.
     */
    public static function removeByResourceId($resourceId)
    {
        try
        {
            $result = ResourceManagement::getResourceById($resourceId);
            if ($result == FALSE)
                return $result;
            $result = ResourceDBManagement::deleteResource($resourceId);
            if ($result == FALSE)
                throw new DBException("Error on Database Delete Query.");
            return TRUE;
        }
        catch (DBException $e)
        {
            throw $e;
        }
        catch (DBConnectionException $e)
        {
            throw $e;
        }
    }

    /**
     * Returns array indexed by resourceID.
     * Returns empty array if there are no resources.
     */
    public static function getAllResources()
    {
        try
        {
            $resourceArray = ResourceDBManagement::findAllResources();
            return $resourceArray;
        }
        catch (DBException $e)
        {
            throw $e;
        }
        catch (DBConnectionException $e)
        {
            throw $e;
        }

    }

    /**
     *  Returns true if database successfully completes update on
     *  resource with the Resource Id of $resourceId else retruns false.
     */
    public static function changeResourceDetail($resource)
    {
        try 
        {
            $result = ResourceDBManagement::findByResourceId($resource->getResourceId());
            $resultb = ResourceDBManagement::findByOwnerId($resource->getOwnerId());
            if($result == TRUE || $resultb == TRUE)
            {
                 ResourceDBManagement::updateResourceDetail($resource);
                 return TRUE;
            }
        }
        catch (DBException $e)
        {
            throw $e;
        }
        catch (DBConnectionException $e)
        {
            throw $e;
        }
    }
    
    /**
     *   Adds a resource with the data in $resource as identified by the Resource
     *   the Resource ID in $resource.
     *   Throws DBExeption if insert is unsuccessful.
     */
    public static function addResource($resource)
    {
        try
        {
            //$result = ResourceDBManagement::findByResourceId($resource->getResourceId());
            //$resultb = ResourceDBManagement::findByOwnerId($resource->getOwnerId());
            
            //if ($result == FALSE && $resultb == FALSE)
            //{
                ResourceDBManagement::insertResourceDetail($resource);
                return TRUE;
            //}
            //else
            //{
                //return FALSE;
            //}
        }
        catch (DBException $e)
        {
            throw $e;
        }
        catch (DBConnectionException $e)
        {
            throw $e;
        }
    }
}
?>
